﻿
using System; 
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Rogan.DBUtility;
using Rogan.Common.Model;

// ReSharper disable once CheckNamespace
namespace Rogan.Dal  
{
	 	//SysRoleDal
		// ReSharper disable once PartialTypeWithSinglePart
	public partial class SysRoleDal
	{
   		     
		public bool Exists(int RoleID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Sys_Role");
			strSql.Append(" where ");
			                                       strSql.Append(" RoleID = @RoleID  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int,4)
			};
			parameters[0].Value = RoleID;

			return DbHelper.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(SysRole model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Sys_Role(");			
            strSql.Append("RoleName,RoleDesc,IsEnable");
			strSql.Append(") values (");
            strSql.Append("@RoleName,@RoleDesc,@IsEnable");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@RoleName", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@RoleDesc", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@IsEnable", SqlDbType.Bit,1)             
              
            };
			            
            parameters[0].Value = model.RoleName;                        
            parameters[1].Value = model.RoleDesc;                        
            parameters[2].Value = model.IsEnable;                        
			   
			object obj = DbHelper.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			                    
        	return Convert.ToInt32(obj);
                                                      
						   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SysRole model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Sys_Role set ");
			                                                
            strSql.Append(" RoleName = @RoleName , ");                                    
            strSql.Append(" RoleDesc = @RoleDesc , ");                                    
            strSql.Append(" IsEnable = @IsEnable  ");            			
			strSql.Append(" where RoleID=@RoleID ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@RoleID", SqlDbType.Int,4) ,            
                        new SqlParameter("@RoleName", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@RoleDesc", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@IsEnable", SqlDbType.Bit,1)             
              
            };
						            
            parameters[0].Value = model.RoleID;                        
            parameters[1].Value = model.RoleName;                        
            parameters[2].Value = model.RoleDesc;                        
            parameters[3].Value = model.IsEnable;                        
            int rows=DbHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			return false;
			
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int RoleID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Sys_Role ");
			strSql.Append(" where RoleID=@RoleID");
						SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int,4)
			};
			parameters[0].Value = RoleID;


			int rows=DbHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			return false;
			
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string RoleIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Sys_Role");
			strSql.Append(" where RoleID in ("+RoleIDlist + ")  ");
			int rows=DbHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}	
			return false;
			
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SysRole GetModel(int RoleID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select RoleID, RoleName, RoleDesc, IsEnable  ");			
			strSql.Append("  from Sys_Role ");
			strSql.Append(" where RoleID=@RoleID");
						SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int,4)
			};
			parameters[0].Value = RoleID;

			
			DataSet ds=DbHelper.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			return null;
			
		}
		
		/// <summary>
		/// DataRow转成实体对象
		/// </summary>
		public SysRole DataRowToModel(DataRow dataRow)
		{
			SysRole model=new SysRole();
											if(dataRow["RoleID"].ToString()!="")
				{
					model.RoleID=int.Parse(dataRow["RoleID"].ToString());
				}
																																				model.RoleName= dataRow["RoleName"].ToString();
																																model.RoleDesc= dataRow["RoleDesc"].ToString();
																																												if(dataRow["IsEnable"].ToString()!="")
				{
					if((dataRow["IsEnable"].ToString()=="1")||(dataRow["IsEnable"].ToString().ToLower()=="true"))
					{
					model.IsEnable= true;
					}
					else
					{
					model.IsEnable= false;
					}
				}
														
				return model;
		}
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataTable GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM Sys_Role ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelper.Query(strSql.ToString()).Tables[0];
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataTable GetList(int top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(top>0)
			{
				strSql.Append(" top "+top);
			}
			strSql.Append(" * ");
			strSql.Append(" FROM Sys_Role ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelper.Query(strSql.ToString()).Tables[0];
		}
	}
}

